1
|
|
|
// Larger scale => smaller graph in the svg viewBox |
2
|
|
|
var scale = 1.1; |
3
|
|
|
var width = scale * window.innerWidth; |
4
|
|
|
var height = scale * window.innerHeight; |
5
|
|
|
|
6
|
|
|
// Set the color cycle for the nodes |
7
|
|
|
var color = d3.scaleOrdinal(d3.schemeCategory10); |
|
|
|
|
8
|
|
|
|
9
|
|
|
var svg = d3.select("body") |
10
|
|
|
.append("svg") |
11
|
|
|
.attr("preserveAspectRatio", "xMinYMin meet") |
12
|
|
|
.attr("viewBox", [0, 0, width, height]) |
13
|
|
|
.classed("svg-content", true) |
14
|
|
|
// Enable zoom and pan |
15
|
|
|
.call(d3.zoom().on("zoom", function (event) { |
16
|
|
|
$("g.nodes").attr("transform", event.transform); |
17
|
|
|
$("g.links").attr("transform", event.transform); |
18
|
|
|
})); |
19
|
|
|
|
20
|
|
|
// Load the link data into the svg |
21
|
|
|
var link = svg.append("g") |
22
|
|
|
.attr("class", "links") |
23
|
|
|
.selectAll("line") |
24
|
|
|
.data(graph.links) |
|
|
|
|
25
|
|
|
.enter().append("line") |
26
|
|
|
.attr("stroke", "#aaa"); |
27
|
|
|
|
28
|
|
|
// Load the node data into the svg |
29
|
|
|
var node = svg.append("g") |
30
|
|
|
.attr("class", "nodes") |
31
|
|
|
.selectAll("g") |
32
|
|
|
.data(graph.nodes) |
33
|
|
|
.enter().append("g"); |
34
|
|
|
|
35
|
|
|
var circles = node.append("a") |
36
|
|
|
.attr("xlink:href", function(d) { return "map_galaxy.php?galaxy_id=" + d.id; }) |
37
|
|
|
.append("circle") |
38
|
|
|
.attr("r", function(d) { return Math.sqrt(d.size); }) |
39
|
|
|
.attr("fill", function(d) { return color(d.group); }) |
40
|
|
|
.call(d3.drag() |
41
|
|
|
.on("start", dragstarted) |
42
|
|
|
.on("drag", dragged) |
43
|
|
|
.on("end", dragended)); |
44
|
|
|
|
45
|
|
|
var labels = node.append("text") |
46
|
|
|
.text(function(d) { return d.name; }) |
47
|
|
|
.attr("font-family", "sans-serif") |
48
|
|
|
.style("fill", "white"); |
49
|
|
|
|
50
|
|
|
var simulation = d3.forceSimulation() |
51
|
|
|
.force("link", d3.forceLink() |
52
|
|
|
.distance(100) |
53
|
|
|
.id(function(d) { return d.name; })) |
54
|
|
|
.force("charge", d3.forceManyBody().strength(-500)) |
55
|
|
|
.force("center", d3.forceCenter(width / 2, height / 2)); |
56
|
|
|
|
57
|
|
|
// Load the node data into the simulation |
58
|
|
|
simulation |
59
|
|
|
.nodes(graph.nodes); |
60
|
|
|
|
61
|
|
|
// Load the link data into the simulation |
62
|
|
|
simulation.force("link") |
63
|
|
|
.links(graph.links); |
64
|
|
|
|
65
|
|
|
// Set the listener for tick events |
66
|
|
|
simulation.on("tick", function () { |
67
|
|
|
link |
68
|
|
|
.attr("x1", function(d) { return d.source.x; }) |
69
|
|
|
.attr("y1", function(d) { return d.source.y; }) |
70
|
|
|
.attr("x2", function(d) { return d.target.x; }) |
71
|
|
|
.attr("y2", function(d) { return d.target.y; }); |
72
|
|
|
|
73
|
|
|
// Ideally we would modify the "transform" attribute of the node |
74
|
|
|
// variable using "translate", but this creates jittery movement |
75
|
|
|
// (probably a browser bug), so we instead reposition the circles |
76
|
|
|
// and labels individually. |
77
|
|
|
circles |
78
|
|
|
.attr("cx", function(d) { return d.x; }) |
79
|
|
|
.attr("cy", function(d) { return d.y; }); |
80
|
|
|
|
81
|
|
|
labels |
82
|
|
|
.attr("x", function(d) { return d.x + 10; }) |
83
|
|
|
.attr("y", function(d) { return d.y + 4; }); |
84
|
|
|
}); |
85
|
|
|
|
86
|
|
|
function dragstarted(event, d) { |
87
|
|
|
if (!event.active) { |
88
|
|
|
simulation.alphaTarget(0.3).restart(); |
89
|
|
|
} |
90
|
|
|
d.fx = d.x; |
91
|
|
|
d.fy = d.y; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
function dragged(event, d) { |
95
|
|
|
d.fx = event.x; |
96
|
|
|
d.fy = event.y; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
function dragended(event, d) { |
100
|
|
|
if (!event.active) { |
101
|
|
|
simulation.alphaTarget(0); |
102
|
|
|
} |
103
|
|
|
d.fx = null; |
104
|
|
|
d.fy = null; |
105
|
|
|
} |
106
|
|
|
|
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.